package com.cqc.googleplay.view;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ImageView;

public class RatioImageView extends ImageView {

	private float ratio =0f;
	private int makeMeasureSpec;

	public RatioImageView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	public RatioImageView(Context context, AttributeSet attrs) {
		super(context, attrs);
		String namespace = "http://schemas.android.com/apk/res/ratioimageview";
		ratio = attrs.getAttributeFloatValue(namespace, "ratio", 2.42f);
	}

	public RatioImageView(Context context) {
		super(context);
	}
	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		int width = MeasureSpec.getSize(widthMeasureSpec);
		if (ratio != 0) {
			float height = width / ratio;
			heightMeasureSpec = MeasureSpec.makeMeasureSpec((int) height, MeasureSpec.EXACTLY);
		} 
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
	}
}
